package com.medical.manage.sync;

import com.medical.common.core.utils.bean.BeanUtils;
import com.medical.manage.domain.Advice;
import com.medical.manage.es.AdviceEs;
import com.medical.manage.esMapper.AdviceEsMapper;
import com.medical.manage.service.AdviceService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class SyncAdviceEs implements ApplicationRunner {
    @Autowired
    private AdviceEsMapper adviceEsMapper;
    @Autowired
    private AdviceService adviceService;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        //先查询要同步的数据
        List<Advice> list = adviceService.list();
        //创建新的集合
        ArrayList<AdviceEs> arrayEsList = new ArrayList<>();
        //遍历要同步的数据
        list.forEach(item->{
            AdviceEs adviceEs = new AdviceEs();
            BeanUtils.copyProperties(item,adviceEs);
            arrayEsList.add(adviceEs);
        });
        adviceEsMapper.insertBatch(arrayEsList);
        log.info("建议同步成功");
    }
}
